<div id="obsah">
 
    <script type="text/javascript">
        $(document).ready(function() {
            // nacitaj graf
            var filled = '<?php echo $filled; ?>'; 
            var taskId = '<?php echo $this->uri->segment(3); ?>';
            var userId = '<?php echo $this->session->userdata('id'); ?>';
            updateTable(filled);
            if (filled != '') {
                refreshGraph(taskId);
            }
            
           $("#save").click(function(event) {
               event.preventDefault();
             var result = tableAsObj(10, <?php echo $task->functions_count+1; ?>);
             var jsonized = JSON.stringify(result);
             
             if (userId != '') { // ak je prihlaseny uloz do databazy
               $.post("<?php echo base_url()."/tasks/editSave" ?>", { json: jsonized, task_id:taskId  }, function() {
                });  
             }
             
             // vykresli
               var encoded = escape(jsonized);
               $('img#graf').attr('src', '<?php echo base_url(); ?>tasks/toGraph/'+taskId+'/'+encoded);
           });
           
             $("#pdf").click(function(event) {
                 event.preventDefault();
                 var result = tableAsObj(10, <?php echo $task->functions_count+1; ?>);
                 var jsonized = JSON.stringify(result);
                 var encoded = escape(jsonized);
                 window.location.href='<?php echo base_url(); ?>tasks/solutionToPdf/'+taskId+'/'+encoded;
            });
            
            // nastav text Ulozit tlacidla podla toho ci je aleob nie je prihlaseny
            if (userId != '') {
                $("#save").button('option','label','Uložiť');
            } else {
                $("#save").button('option','label','Vykresliť');
            }
            
        });
        
        function refreshGraph(taskId) {
            var result = tableAsObj(10, <?php echo $task->functions_count+1; ?>);
            var jsonized = JSON.stringify(result);
            var encoded = escape(jsonized);
            $('img#graf').attr('src', '<?php echo base_url(); ?>tasks/toGraph/'+taskId+'/'+encoded);
        }
        
        
        
    </script>
    
<?php
echo "<h3>$task->name</h3>";
echo "<h4>$task->book_name (strana $task->page)</h4>";
echo "<hr/>";
echo "<strong>Popis:</strong><br/>$task->description";
echo "<br/>";

echo "<h4> Hodnoty </h4>";
$table = '<table border="1" style="width: 100%">';
        
        for ($i=0;$i<$task->functions_count+1;$i++) {
            $table .= "<tr>";
            if ($i==0) {
                $table .= "<td style='width: 100px;'>x [$task->x_units]</td>";
            } else {
                $table .= "<td style='width: 100px;'>f$i(x) [$task->y_units]</td>";
            }
            
            for ($j=0;$j<10;$j++) {
               $table .= "<td><input id='".$i.'_'.$j."' type='text' style='width: 70px;' class='float' />"."</td>";
            }
            $table .="</tr>";
        }
        $table .= '</table>';
        
 echo $table;
 
   echo "<a class='button' href='#' id='save'>Uložiť</a>";
  echo "<a class='button' href='#' id='pdf'>PDF</a>"; 
  
  
 echo "<h4> Graf </h4>";
 echo "<img id='graf' alt='Graf' /> <br/>";

 
 ?>

</div>